package cn.jinjimi.app.main.adapter.recycler;

import android.animation.Animator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.Item.ZdbFilterItem;
import cn.jinjimi.app.interfaces.IRVGridHeaderable;

/**
 * Created by Roy
 * Date: 2017/3/20
 */

public class ZdbProductFilterAdapter extends MyBaseRecyclerAdapter<ZdbFilterItem, ZdbProductFilterAdapter.ViewHolder> implements IRVGridHeaderable {

    private Context mContext;
    private final LayoutInflater mLayoutInflater;

    public ZdbProductFilterAdapter(Context context) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    protected Animator[] getAnimators(View view) {
        return new Animator[0];
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).itemType;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ZdbFilterItem.TypeHeader:
                return new HeadHolder(mLayoutInflater.inflate(R.layout.zdb_filter_head_item, parent, false));
            case ZdbFilterItem.TypeCompany:
            case ZdbFilterItem.TypeInsureClass:
            case ZdbFilterItem.TypeAge:
                return new ItemHolder(mLayoutInflater.inflate(R.layout.zdb_filter_item_item, parent, false));
            default:
                return new HeadHolder(mLayoutInflater.inflate(R.layout.zdb_filter_head_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ZdbFilterItem item = getItem(position);
        holder.setData(item);
    }

    @Override
    public boolean isHeader(int position) {
        return getItemCount() > 0 ? getItemViewType(position) == ZdbFilterItem.TypeHeader : false;
    }

    class ViewHolder extends MyBaseRecyclerAdapter.MyViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }

        public void setData(ZdbFilterItem item) {
        }
    }

    class HeadHolder extends ViewHolder {

        private TextView tvTitle;

        public HeadHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }

        @Override
        public void setData(ZdbFilterItem item) {
            super.setData(item);
            tvTitle.setText(item.headName);
        }
    }

    class ItemHolder extends ViewHolder {

        private TextView tvName;

        public ItemHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
        }

        @Override
        public void setData(ZdbFilterItem item) {
            super.setData(item);
            switch (item.itemType) {
                case ZdbFilterItem.TypeCompany:
                    if (item.company != null && item.company.getCompanyName() != null)
                        tvName.setText(item.company.getCompanyName());
                    break;
                case ZdbFilterItem.TypeInsureClass:
                    if (item.zdbInsurance != null && item.zdbInsurance.getName() != null)
                        tvName.setText(item.zdbInsurance.getName());
                    break;
                case ZdbFilterItem.TypeAge:
                    if (item.zdbAge != null && item.zdbAge.name != null)
                        tvName.setText(item.zdbAge.name);
                    break;
            }
            tvName.setSelected(item.isSelected);
            tvName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onActionClickListener != null)
                        onActionClickListener.onItemClick(getHolderPosition());
                }
            });
        }
    }

    public onActionClickListener onActionClickListener;

    public void setOnActionClickListener(onActionClickListener listener) {
        this.onActionClickListener = listener;
    }

    public interface onActionClickListener {
        void onItemClick(int position);
    }
}
